home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 4.iso
/
src
/
haeberli
/
libgutil
/
fit2d.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-01
|
3KB
|
130 lines
/*
* Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
* All Rights Reserved.
*
* This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
* the contents of this file may not be disclosed to third parties, copied or
* duplicated in any form, in whole or in part, without the prior written
* permission of Silicon Graphics, Inc.
*
* RESTRICTED RIGHTS LEGEND:
* Use, duplication or disclosure by the Government is subject to restrictions
* as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
* and Computer Software clause at DFARS 252.227-7013, and/or in similar or
* successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
* rights reserved under the Copyright Laws of the United States.
*/
/*
* fit2d -
* fit a plane to a 2D scalar function.
*
* Paul Haeberli with Tom Davis
*/
#include "stdio.h"
static double m00, m01, m02;
static double m10, m11, m12;
static double m20, m21, m22;
static double v0, v1, v2;
static double A, B, C;
static int havemat;
initfit()
{
m00 = m01 = m02 = 0.0;
m10 = m11 = m12 = 0.0;
m20 = m21 = m22 = 0.0;
v0 = v1 = v2 = 0.0;
havemat = 0;
}
fitpoint(x,y,r)
float x, y, r;
{
m00 += x*x;
m01 += x*y;
m02 += x;
m10 += x*y;
m11 += y*y;
m12 += y;
m20 += x;
m21 += y;
m22 += 1.0;
v0 += r*x;
v1 += r*y;
v2 += r;
havemat = 0;
}
float getfitval(x,y)
float x, y;
{
float inpos, outpos;
float mat[4][4], imat[4][4];
float a, b, c;
if(!havemat) {
bzero(mat,16*sizeof(float));
mat[0][0] = m00;
mat[0][1] = m01;
mat[0][2] = m02;
mat[1][0] = m10;
mat[1][1] = m11;
mat[1][2] = m12;
mat[2][0] = m20;
mat[2][1] = m21;
mat[2][2] = m22;
mat[3][3] = 1.0;
if(m22<0.5) {
A = 0.0;
B = 0.0;
C = 0.0;
} else if(invertmat(mat,imat)) {
xformpnt(imat,v0,v1,v2,&a,&b,&c);
A = a;
B = b;
C = c;
} else {
A = 0.0;
B = 0.0;
C = v2/m22;
}
havemat = 1;
}
return A*x+B*y+C;
}
#ifdef TESTIT
printval(x,y)
float x, y;
{
printf("val iat %f %f is %f\n",x,y,getfitval(x,y));
}
main()
{
initfit();
fitpoint(0.0,0.0,0.0);
fitpoint(1.0,0.0,1.0);
fitpoint(1.0,0.0,1.0);
fitpoint(1.0,0.0,1.0);
fitpoint(1.0,0.0,1.0);
fitpoint(1.0,0.0,1.0);
fitpoint(1.0,0.0,1.0);
fitpoint(1.0,0.0,1.0);
fitpoint(1.0,0.0,1.0);
fitpoint(1.0,0.0,1.0);
fitpoint(1.0,0.0,1.0);
fitpoint(1.0,0.0,1.0);
fitpoint(0.0,1.0,1.0);
fitpoint(1.0,1.0,2.0);
printval(0.0,0.0);
printval(1.0,0.0);
printval(0.0,1.0);
printval(1.0,1.0);
printval(0.1,0.0);
printval(0.5,0.5);
}
#endif